//
//  JCContacterCell.swift
//  JChat
//
//  Created by JIGUANG on 2017/3/14.
//  Copyright © 2017年 HXHG. All rights reserved.
//

import UIKit
import MTCategory

class JCContacterCell: UITableViewCell {
    
    var isShowBadge: Bool {
        get {
            return !redPoin.isHidden
        }
        set {
            redPoin.isHidden = !newValue
        }
    }
    
    var icon: UIImage? {
        get {
            return avatorView.image
        }
        set {
            avatorView.layer.cornerRadius = 5
            avatorView.layer.masksToBounds = true
            avatorView.image = newValue
        }
    }
    
    var title: String? {
        get {
            return usernameLabel.text
        }
        set {
            usernameLabel.text = newValue
        }
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //contentView.backgroundColor = UIColor.theme.background
        //backgroundColor = UIColor.theme.background
        _init()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        _init()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        _init()
    }

    private lazy var avatorView: UIImageView = UIImageView()
    private lazy var usernameLabel: UILabel = UILabel()
    private lazy var redPoin: UILabel = UILabel()
    
    public func bindDate(_ user : JMSGUser) {
        self.title = user.displayName()
//        user.thumbAvatarData({ (data, name, error) in
//            if let data = data {
//                let image = UIImage(data: data)
//                self.icon = image
//            } else {
//                self.icon = UIImage.loadImage("com_icon_user_36")
//            }
//        })
        self.avatorView.layer.cornerRadius = 5
        self.avatorView.layer.masksToBounds = true
        self.avatorView.mtSetImage(user.avatar, nil)
    }
    
    public func bindDateWithGroup(group : JMSGGroup) {
        title = group.displayName()
        group.thumbAvatarData { (data, id, error) in
            if let data = data {
                self.icon = UIImage(data: data)
            } else {
                self.icon = UIImage(named: defaultPlaceholderIcon60x60)
            }
        }
    }
    
    
    lazy var lineView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
        return v
    }()
    
    //MARK: - private func
    private func _init() {
        selectionStyle = .none
        contentView.backgroundColor = UIColor.white
        usernameLabel.textColor = UIColor.text.x1d1a1a
        usernameLabel.font = PingFangSCSemiboldFont(14)
//        usernameLabel.backgroundColor = .white
//        usernameLabel.layer.masksToBounds = true

        redPoin.textAlignment = .center
        redPoin.text = ""
        redPoin.layer.cornerRadius = 4.0
        redPoin.layer.masksToBounds = true
        redPoin.isHidden = true
        redPoin.layer.backgroundColor = UIColor.red.cgColor
        
        contentView.addSubview(avatorView)
        contentView.addSubview(usernameLabel)
        contentView.addSubview(redPoin)
        contentView.addSubview(lineView)
        
        avatorView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(44)
        }
        
        usernameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(avatorView.snp.right).offset(11)
            //make.bottom.equalTo(contentView.snp.centerY).offset(-2)
            make.centerY.equalToSuperview()
        }
        
        
        redPoin.snp.makeConstraints { (make) in
            make.left.equalTo(avatorView.snp.right).offset(-5)
            make.top.equalTo(avatorView).offset(-3)
            make.width.height.equalTo(8)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.bottom.equalToSuperview()
            make.right.equalToSuperview().offset(20)
            make.height.equalTo(0.5)
        }
    }

}
